/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 pjoc.pub, blademainer.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package pub.pjoc.pay.service;

import pub.pjoc.pay.dao.ConfigDao;
import pub.pjoc.pay.pojo.ConfigInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author xiongyingqi
 * @since 16-6-8 下午5:40
 */
@Service
public class ConfigService {
  private static final Logger logger = LoggerFactory
      .getLogger(ConfigService.class);
  @Autowired
  private ConfigDao configDao;

  public String setConfig(String configGroup, String configName, String configValue) {
    ConfigInfo configInfo = new ConfigInfo();
    configInfo.setConfigGroup(configGroup);
    configInfo.setConfigName(configName);
    configInfo.setConfigValue(configValue);
    ConfigInfo configFromDb = getConfigFromDb(configGroup, configName);
    int i = configDao.updateConfigByConfigNameAndValue(configInfo);
    if (i != 1) {
      logger.warn("Update config abnormal! configValue: {}, configGroup: {}, configName: {}",
          configValue, configGroup, configName);
    }
    return configFromDb.getConfigValue();
  }

  public String getConfig(String configGroup, String configName) {
    Assert.hasText(configGroup);
    Assert.hasText(configName);
    ConfigInfo configFromDb = getConfigFromDb(configGroup, configName);
    if (configFromDb == null) {
      logger.error(
          "Getting config from database error, cause does'nt exists ConfigGroup: {} and ConfigName: {}",
          configGroup, configName);
      return null;
    }

    if (logger.isDebugEnabled()) {
      logger.debug("Got config: {} from database.", configFromDb);
    }

    String configValue = configFromDb.getConfigValue();
    if (configValue == null) {
      return null;
    }
    return configValue;
  }

  public List<ConfigInfo> getConfigByName(String configName) {
    List<ConfigInfo> configInfoListByConfigName = configDao.getConfigInfoListByConfigName(configName);
    return configInfoListByConfigName;
  }


  public ConfigInfo getConfigFromDb(String configGroup, String configName) {
    ConfigInfo configInfo = new ConfigInfo();
    configInfo.setConfigGroup(configGroup);
    configInfo.setConfigName(configName);

    ConfigInfo config = configDao.getConfig(configInfo);
    return config;
  }

  public boolean clearCache() {
    return true;
  }
}
